var canvas = document.getElementById("application");

// Create the app and start the update loop
var app = new pc.Application(canvas, {
    elementInput: new pc.ElementInput(canvas),
    mouse: new pc.Mouse(canvas),
    touch: new pc.TouchDevice(canvas),
    keyboard: new pc.Keyboard(window)
});
app.start();

// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);

app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2);

/**
 * 需要加载的资源, 包括js脚本
 */
var assets = new Map();

/**
 * 加载资源
 */
function loadAssets() {
    var toloadAssets = [
        {
            name: 'bag',
            type: 'model',
            url: './res/Bag/Bag.json'
        },
        {
            name: 'background',
            type: 'texture',
            url: './res/Background/background.jpg'
        },
        {
            name: 'firstPersonCamera',
            type: 'script',
            url: './src/firstPersonCamera.js'
        },
        {
            name: 'bagController',
            type: 'script',
            url: './src/bagController.js'
        },
        {
            name: 'backgroundScript',
            type: 'script',
            url: './src/background.js'
        }
    ];

    var loadingIndex = 0;
    var isLoading = false;
    var loadingHandle = -1;

    var loadFromUrl = function () {
        if (isLoading)
            return;

        isLoading = true;
        app.assets.loadFromUrl(toloadAssets[loadingIndex].url, toloadAssets[loadingIndex].type, function (err, asset) {
            console.log(asset);
            assets.set(toloadAssets[loadingIndex].name, asset);
            ++loadingIndex;

            // 判断加载进度
            var progress = loadingIndex / toloadAssets.length;
            if (loadingIndex == toloadAssets.length) {
                clearInterval(loadingHandle);
                onLoadComplete();
            }

            isLoading = false;
        });
    };

    loadingHandle = setInterval(loadFromUrl, 20);
}

var entity = null;
var light = null;
var camera = null;

/**
 * 加载完毕需要做的事情
 */
function onLoadComplete() {
    // 背景是一张静态的图
    var background = new pc.Entity('background');
    app.root.addChild(background);
    background.addComponent('script');
    background.script.create('background');

    // 模型加入世界
    entity = new pc.Entity();
    entity.addComponent("model", {
        type: "asset",
        asset: assets.get('bag'),
        castShadows: true
    });
    app.root.addChild(entity);
    entity.translate(0, 7, 22);
    entity.addComponent('script');
    entity.script.create("bagController");

    // Create an Entity with a camera component
    var camera = new pc.Entity();
    camera.addComponent("camera", {
        clearColor: new pc.Color(0.4, 0.45, 0.5),
        farClip: 1000,
        nearClip: 0.3,
        fov: 60
    });
    camera.addComponent('script');
    // camera.script.create("firstPersonCamera");
    camera.translate(0, 7, 29);
    app.root.addChild(camera);

    // Create an Entity with a point light component
    var light = new pc.Entity();
    light.addComponent("light", {
        type: "point",
        color: new pc.Color(1, 1, 1),
        range: 100,
        castShadows: true
    });

    // 灯光绑定在Camera上
    camera.addChild(light);

    app.on("update", function (dt) {
        if (entity) {
        }
    });
}

loadAssets();